package com.hwtx.form.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.hwtx.form.annotation.PerfEnable;
import com.hwtx.form.domain.repo.FormConfigRepo;
import com.hwtx.form.domain.vo.FormConfigQuerySearchVo;
import com.hwtx.form.domain.vo.FormConfigQueryVo;
import com.hwtx.form.persistence.entity.FormConfigQuery;
import com.hwtx.form.persistence.entity.FormConfigTpl;
import com.hwtx.form.persistence.entity.FormDefDetail;
import com.hwtx.form.persistence.mapper.FormConfigQueryMapper;
import com.hwtx.form.persistence.mapper.FormConfigTplMapper;
import com.hwtx.form.persistence.mapper.FormDefDetailMapper;
import io.geekidea.boot.framework.page.Paging;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class FormConfigRepoImpl implements FormConfigRepo {
    @Resource
    private FormDefDetailMapper formDefDetailMapper;
    @Resource
    private FormConfigQueryMapper formConfigQueryMapper;
    @Resource
    private FormConfigTplMapper formConfigTplMapper;

    @Override
    public FormDefDetail getFormDetail(String keyId) {
        LambdaQueryWrapper<FormDefDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormDefDetail::getKeyId, keyId);
        return formDefDetailMapper.selectOne(wrapper);
    }

    @Override
    public FormDefDetail getFormDetail(Long formId) {
        LambdaQueryWrapper<FormDefDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormDefDetail::getFormId, formId);
        return formDefDetailMapper.selectOne(wrapper);
    }

    @Override
    public List<FormDefDetail> getFormDetailByModelName(String modelName) {
        LambdaQueryWrapper<FormDefDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormDefDetail::getModelName, modelName);
        return formDefDetailMapper.selectList(wrapper);
    }

    @Override
    public int updateFormDefDetail(FormDefDetail formDefDetail) {
        return formDefDetailMapper.update(formDefDetail, new LambdaQueryWrapper<FormDefDetail>().eq(FormDefDetail::getFormId, formDefDetail.getFormId()));
    }

    @Override
    public int saveFormDefDetail(FormDefDetail formDefDetail) {
        return formDefDetailMapper.insert(formDefDetail);
    }

    @Override
    public void saveFormConfigQuery(FormConfigQuery query) {
        formConfigQueryMapper.insert(query);
    }

    @Override
    public int updateFormConfigQuery(FormConfigQuery formConfigQuery) {
        return formConfigQueryMapper.updateById(formConfigQuery);
    }

    @Override
    public void updateFormConfigQueryListFormQueryShowStatus(Long formId, FormConfigQuery query) {
        LambdaUpdateWrapper<FormConfigQuery> wrapper = new LambdaUpdateWrapper<>();
        if (query.getIsFormShow() != null) {
            wrapper.set(FormConfigQuery::getIsFormShow, query.getIsFormShow());
        }
        if (query.getIsQueryShow() != null) {
            wrapper.set(FormConfigQuery::getIsQueryShow, query.getIsQueryShow());
        }
        if (query.getIsListShow() != null) {
            wrapper.set(FormConfigQuery::getIsListShow, query.getIsListShow());
        }
        if (query.getIsListQuery() != null) {
            wrapper.set(FormConfigQuery::getIsListQuery, query.getIsListQuery());
        }
        wrapper.eq(FormConfigQuery::getFormId, formId);
        formConfigQueryMapper.update(wrapper);
    }

    @Override
    public FormConfigQuery getFormConfigQuery(Long id) {
        return formConfigQueryMapper.selectById(id);
    }

    @Override
    public List<FormConfigQuery> listFormConfigQuery(FormConfigQuerySearchVo searchVo) {
        LambdaQueryWrapper<FormConfigQuery> wrapper = new LambdaQueryWrapper<>();
        if (searchVo.getFormId() != null) {
            wrapper.eq(FormConfigQuery::getFormId, searchVo.getFormId());
        }
        if (searchVo.getIsListQuery() != null) {
            wrapper.eq(FormConfigQuery::getIsListQuery, searchVo.getIsListQuery());
        }
        if (CollectionUtils.isNotEmpty(searchVo.getIds())) {
            wrapper.in(FormConfigQuery::getId, searchVo.getIds());
        }
        if (searchVo.getNum() != null) {
            wrapper.eq(FormConfigQuery::getNum, searchVo.getNum());
        }
        if (searchVo.getName() != null) {
            wrapper.eq(FormConfigQuery::getName, searchVo.getName());
        }
        return formConfigQueryMapper.selectList(wrapper);
    }

    @Override
    public List<FormConfigQuery> listFormConfigQueryWithExt() {
        return formConfigQueryMapper.selectList(new LambdaQueryWrapper<FormConfigQuery>().isNotNull(FormConfigQuery::getExt));
    }

    @PerfEnable
    @Override
    public Paging<FormConfigQueryVo> pageMergedFormConfig(int pageIndex, int pageSize, FormConfigQuerySearchVo searchVo) {
        PageHelper.startPage(pageIndex, pageSize);
        List<FormConfigQueryVo> formConfigQueryVos = formConfigQueryMapper.listFormConfigQuery(searchVo);
        return new Paging<>(formConfigQueryVos);
    }

    @Override
    @PerfEnable
    public List<FormConfigQueryVo> listMergedFormConfig(FormConfigQuerySearchVo searchVo) {
        return formConfigQueryMapper.listFormConfigQuery(searchVo);
    }

    @Override
    public void deleteFormConfigQuery(Long formId) {
        formConfigQueryMapper.delete(new LambdaQueryWrapper<FormConfigQuery>().eq(FormConfigQuery::getFormId, formId));
    }

    @Override
    public void deleteFormConfigQueryById(Long id) {
        formConfigQueryMapper.deleteById(id);
    }

    @Override
    public void deleteFormDetail(Long formId) {
        formDefDetailMapper.delete(new LambdaQueryWrapper<FormDefDetail>().eq(FormDefDetail::getFormId, formId));
    }

    @Override
    public FormConfigTpl getFormTplConfig(Long formId) {
        return formConfigTplMapper.selectOne(new LambdaQueryWrapper<FormConfigTpl>().eq(FormConfigTpl::getFormId, formId));
    }

    @Override
    public boolean saveFormTplConfig(FormConfigTpl formConfigTpl) {
        return formConfigTplMapper.insert(formConfigTpl) > 0;
    }

    @Override
    public boolean updateFormTplConfig(FormConfigTpl formConfigTpl) {
        return formConfigTplMapper.updateById(formConfigTpl) > 0;
    }

}
